package com.yaozheng.vocationaltraining.view;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.iview.IItemView;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 一项试卷目录
 *
 * @author Administrator
 */
@EViewGroup(R.layout.item_paper_catalog)
public class PaperCatalogItemView extends LinearLayout {
    /**
     * 试卷状态
     */
    @ViewById
    ExamProgressStatusView examProgressStatusView;
    @ViewById
    TextView paperTitleText, paperDurationText, passingScoreText;

    IItemView iItemView;

    int position;

    private JSONObject currPaperJsonObject;


    public PaperCatalogItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public PaperCatalogItemView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    /**
     * 初始化
     *
     * @param iItemView
     */
    public void init(IItemView iItemView) {
        this.iItemView = iItemView;
    }

    public void loadData(int position, JSONObject paperJsonObject) {
        this.position = position;
        this.currPaperJsonObject = paperJsonObject;
        paperTitleText.setText(TypeUtils.getJsonString(paperJsonObject,
                "subject", ""));
        paperDurationText.setText(getResources().getString(
                R.string.assignmentDurationStr,
                TypeUtils.getJsonString(paperJsonObject, "startTime", "")));
//        passingScoreText.setText(getResources().getString(
//                R.string.passingScoreStr,
//                TypeUtils.getJsonInteger(paperJsonObject, "passing_score", 0)));
        int status = TypeUtils.getJsonInteger(paperJsonObject, "status");
        examProgressStatusView.setVisibility(View.VISIBLE);
        if (status == 0) {

            examProgressStatusView.setVisibility(View.GONE);

        } else if (status == 2) {
            // 分数

            examProgressStatusView.setStatus(TypeUtils.getJsonInteger(
                    paperJsonObject, "score"));
        } else if (status == 1) {
            // 考试中
            examProgressStatusView.setStatus(-2);

        } else {
            // 考试
            examProgressStatusView.setStatus(-1);
        }
    }

    /**
     * 进入考试页面
     */
    @Click(R.id.examProgressStatusView)
    public void enterExamPage() {
        int status = TypeUtils.getJsonInteger(currPaperJsonObject, "status");
        if (status == 1) {
            iItemView.itemClick(position);
        }

        // Intent intent = new Intent();
        // intent.setClassName(getContext(),
        // Constants.ONLINE_EXAM_ACTIVITY_CLASS_NAME);
        // getContext().startActivity(intent);
    }
}
